import java.math.BigDecimal;
import java.util.BitSet;


public class IPDatagram {

	private String datagramBits;

	public IPDatagram(int version) {

		if (version == 4) {
			datagramBits = assemble(4);
		} else {
			throw new IllegalArgumentException("Datagram not built");
		}
	}

	/**
	 * Assembles an IP Datagram. Maximum length is 65535 bits.
	 */
	private String assemble(int version) {
		StringBuilder dataString = new StringBuilder();

		// Append version, 4 bits. 0100 = 4 decimal.
		dataString.append("0100");

		// Append header length (HLEN), 4 bits. 0101 = 5 decimal.
		dataString.append("0101");

		// TODO: Replace appends with correct values.
		// Append service type, 8 bits.
		dataString.append("10001000");

		// Append total length, 16 bits.
		dataString.append("1000100010001000");

		// Append identification, 16 bits.
		dataString.append("1000100010001000");

		// Append flags, 3 bits.
		dataString.append("111");

		// Append fragment offset, 13 bits.
		dataString.append("1000100010001");

		// Append time to live (Number of hops left), 8 bits.
		dataString.append("10001000");

		// Append protocol, 8 bits. 0110 = 6 decimal. Corresponding to TCP protocol.
		dataString.append("00000110");

		// Append header checksum, 16 bits.
		dataString.append("1000100010001000");

		// Append source IP, 32 bits.
		// TODO: MacAddressToBinaryString.
		dataString.append("10001000");

		// Append destination IP, 32 bits.
		dataString.append("10001000");

		// Append options, variable bit length.

		// Append padding, variable bit length.

		// Append data, variable bit length.

		return dataString.substring(0, dataString.length());
	}
	
	private byte[] StringToBytes () {
		byte[] result = {127, -128, -128, -112, 0, 0};
		
		return result;
	}
	
	public String toString() {
		
		return datagramBits.toString();
	}

}
